<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="staffName">
        <el-input
          v-model="queryParams.staffName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司" prop="company">
        <el-input
          v-model="queryParams.company"
          placeholder="请输入公司"
          :disabled="true"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          v-model="queryParams.departmentName"
          placeholder="请输入部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="岗位" prop="postName">
        <el-input
          v-model="queryParams.postName"
          placeholder="请输入岗位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['module:wageOperate:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['module:wageOperate:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['module:wageOperate:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="opendate(5)"
          v-hasPermi="['module:wageOperate:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-s-custom"
          size="mini"
          @click="handleupdatestaff"
        >更新人员</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-s-custom"
          size="mini"
          @click="opendate(1)"

        >计算考勤</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-s-custom"
          size="mini"
          @click="opendate(2)"
          :disabled="btndisabled"
        >计算薪资</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-s-promotion"
          size="mini"
          @click="opendate(3)"

        >保存</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-s-promotion"
          size="mini"
          @click="opendate(4)"
        >发送</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="wageOperateList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键ID" align="center" prop="id" />-->
      <el-table-column label="序号" type="index" align="center" prop="sequence" />
      <el-table-column label="部门" align="center" prop="departmentName" v-if="columns[0].visible"/>
<!--      <el-table-column label="部门id" align="center" prop="departmentId" />-->
      <el-table-column label="公司" align="center" prop="company" v-if="columns[1].visible"/>
<!--      <el-table-column label="岗位id" align="center" prop="postId" />-->
      <el-table-column label="岗位" align="center" prop="postName" v-if="columns[2].visible"/>
      <el-table-column label="姓名" align="center" prop="staffName" v-if="columns[3].visible"/>
<!--      <el-table-column label="姓名id" align="center" prop="staffId" />-->
      <el-table-column label="入职日期" align="center" prop="entryDate" width="180" v-if="columns[4].visible">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.entryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="离职日期" align="center" prop="leaveDate" width="180" v-if="columns[5].visible">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.leaveDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="员工状态" align="center" prop="staffState" v-if="columns[6].visible"/>
      <el-table-column label="员工类型" align="center" prop="staffType" v-if="columns[7].visible"/>
      <el-table-column label="基本工资" align="center" prop="basiWage" v-if="columns[8].visible"/>
      <el-table-column label="岗位津贴" align="center" prop="postAllowance" v-if="columns[9].visible"/>
      <el-table-column label="忠诚奖励" align="center" prop="loyaltyReward" v-if="columns[10].visible"/>
      <el-table-column label="技能考核" align="center" prop="skillAssessment" v-if="columns[11].visible"/>
      <el-table-column label="管理考核" align="center" prop="managementAssessment" v-if="columns[12].visible"/>
      <el-table-column label="安全奖" align="center" prop="safetyAward" v-if="columns[13].visible"/>
      <el-table-column label="绩效" align="center" prop="performance" v-if="columns[14].visible"/>
      <el-table-column label="全勤" align="center" prop="fullAttendance" v-if="columns[15].visible"/>
      <el-table-column label="出勤天数" align="center" prop="attendanceDays" v-if="columns[16].visible"/>
      <el-table-column label="出勤工资" align="center" prop="attendanceWage" v-if="columns[17].visible"/>
      <el-table-column label="专项补贴" align="center" prop="specialSubsidy" v-if="columns[18].visible"/>
      <el-table-column label="应发工资" align="center" prop="payDue" v-if="columns[19].visible"/>
      <el-table-column label="社保" align="center" prop="socialSecurity" v-if="columns[20].visible"/>
      <el-table-column label="个税" align="center" prop="personalIncomeTax" v-if="columns[21].visible"/>
      <el-table-column label="罚款" align="center" prop="fine" v-if="columns[22].visible"/>
      <el-table-column label="其他" align="center" prop="other" v-if="columns[23].visible"/>
      <el-table-column label="实发工资" align="center" prop="realWages" v-if="columns[24].visible"/>
      <el-table-column label="内部备注" align="center" prop="internalNotes" v-if="columns[25].visible"/>
      <el-table-column label="备注" align="center" prop="remark" v-if="columns[26].visible"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['module:wageOperate:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['module:wageOperate:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工资计算对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="序号" prop="sequence">
          <el-input v-model="form.sequence" placeholder="请输入序号" />
        </el-form-item>
        <el-form-item label="部门" prop="departmentName">
          <el-input v-model="form.departmentName" placeholder="请输入部门" />
        </el-form-item>
        <el-form-item label="部门id" prop="departmentId">
          <el-input v-model="form.departmentId" placeholder="请输入部门id" />
        </el-form-item>
        <el-form-item label="公司" prop="company">
          <el-input v-model="form.company" placeholder="请输入公司" />
        </el-form-item>
        <el-form-item label="岗位id" prop="postId">
          <el-input v-model="form.postId" placeholder="请输入岗位id" />
        </el-form-item>
        <el-form-item label="岗位" prop="postName">
          <el-input v-model="form.postName" placeholder="请输入岗位" />
        </el-form-item>
        <el-form-item label="姓名" prop="staffName">
          <el-input v-model="form.staffName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="姓名id" prop="staffId">
          <el-input v-model="form.staffId" placeholder="请输入姓名id" />
        </el-form-item>
        <el-form-item label="入职日期" prop="entryDate">
          <el-date-picker clearable
            v-model="form.entryDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择入职日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="离职日期" prop="leaveDate">
          <el-date-picker clearable
            v-model="form.leaveDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择离职日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="员工状态" prop="staffState">
          <el-input v-model="form.staffState" placeholder="请输入员工状态" />
        </el-form-item>
        <el-form-item label="基本工资" prop="basiWage">
          <el-input v-model="form.basiWage" placeholder="请输入基本工资" />
        </el-form-item>
        <el-form-item label="岗位津贴" prop="postAllowance">
          <el-input v-model="form.postAllowance" placeholder="请输入岗位津贴" />
        </el-form-item>
        <el-form-item label="忠诚奖励" prop="loyaltyReward">
          <el-input v-model="form.loyaltyReward" placeholder="请输入忠诚奖励" />
        </el-form-item>
        <el-form-item label="技能考核" prop="skillAssessment">
          <el-input v-model="form.skillAssessment" placeholder="请输入技能考核" />
        </el-form-item>
        <el-form-item label="管理考核" prop="managementAssessment">
          <el-input v-model="form.managementAssessment" placeholder="请输入管理考核" />
        </el-form-item>
        <el-form-item label="安全奖" prop="safetyAward">
          <el-input v-model="form.safetyAward" placeholder="请输入安全奖" />
        </el-form-item>
        <el-form-item label="绩效" prop="performance">
          <el-input v-model="form.performance" placeholder="请输入绩效" />
        </el-form-item>
        <el-form-item label="全勤" prop="fullAttendance">
          <el-input v-model="form.fullAttendance" placeholder="请输入全勤" />
        </el-form-item>
        <el-form-item label="出勤天数" prop="attendanceDays">
          <el-input v-model="form.attendanceDays" placeholder="请输入出勤天数" />
        </el-form-item>
        <el-form-item label="出勤工资" prop="attendanceWage">
          <el-input v-model="form.attendanceWage" placeholder="请输入出勤工资" />
        </el-form-item>
        <el-form-item label="专项补贴" prop="specialSubsidy">
          <el-input v-model="form.specialSubsidy" placeholder="请输入专项补贴" />
        </el-form-item>
        <el-form-item label="应发工资" prop="payDue">
          <el-input v-model="form.payDue" placeholder="请输入应发工资" />
        </el-form-item>
        <el-form-item label="社保" prop="socialSecurity">
          <el-input v-model="form.socialSecurity" placeholder="请输入社保" />
        </el-form-item>
        <el-form-item label="个税" prop="personalIncomeTax">
          <el-input v-model="form.personalIncomeTax" placeholder="请输入个税" />
        </el-form-item>
        <el-form-item label="罚款" prop="fine">
          <el-input v-model="form.fine" placeholder="请输入罚款" />
        </el-form-item>
        <el-form-item label="其他" prop="other">
          <el-input v-model="form.other" placeholder="请输入其他" />
        </el-form-item>
        <el-form-item label="实发工资" prop="realWages">
          <el-input v-model="form.realWages" placeholder="请输入实发工资" />
        </el-form-item>
        <el-form-item label="内部备注" prop="internalNotes">
          <el-input v-model="form.internalNotes" placeholder="请输入内部备注" />
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="选择日期"
      :visible.sync="dateshow"
      width="30%"
      @close="closedate"
     >
      <el-form ref="form" :model="checkform" :rules="checkformrules" label-width="80px">
        <el-form-item label="日期" prop="renewalDate">
          <el-date-picker
            v-model="checkform.renewalDate"
            type="month"
            placeholder="选择月">
          </el-date-picker>
        </el-form-item>

      </el-form>
     <span>
    <el-button @click="closedate">取 消</el-button>
    <el-button type="primary" @click="btndate">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
import { listWageOperate, getWageOperate, delWageOperate, addWageOperate, updateWageOperate,updatestaffwagelist,updatecheckapi,updatepayapi,saverecord,summoneyall,sendinfo } from "@/api/module/wageOperate";

export default {
  name: "WageOperate",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工资计算表格数据
      wageOperateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sequence: null,
        departmentName: null,
        departmentId: null,
        company: "玉山分公司",
        postId: null,
        postName: null,
        staffName: null,
        staffId: null,
        entryDate: null,
        leaveDate: null,
        staffState: null,
        staffType: null,
        basiWage: null,
        postAllowance: null,
        loyaltyReward: null,
        skillAssessment: null,
        managementAssessment: null,
        safetyAward: null,
        performance: null,
        fullAttendance: null,
        attendanceDays: null,
        attendanceWage: null,
        specialSubsidy: null,
        payDue: null,
        socialSecurity: null,
        personalIncomeTax: null,
        fine: null,
        other: null,
        realWages: null,
        internalNotes: null,
      },
      columns: [
        { key: 0, label: `部门`, visible: true },
        { key: 1, label: `公司`, visible: true },
        { key: 2, label: `岗位`, visible: true },
        { key: 3, label: `姓名`, visible: true },
        { key: 4, label: `入职日期`, visible: true },
        { key: 5, label: `离职日期`, visible: true },
        { key: 6, label: `员工状态`, visible: true },
        { key: 7, label: `员工类型`, visible: true },
        { key: 8, label: `基本工资`, visible: true },
        { key: 9, label: `岗位精铁`, visible: true },
        { key: 10, label: `忠诚奖励`, visible: true },
        { key: 11, label: `技能考核`, visible: true },
        { key: 12, label: `管理考核`, visible: true },
        { key: 13, label: `安全奖`, visible: true },
        { key: 14, label: `绩效`, visible: true },
        { key: 15, label: `全勤`, visible: true },
        { key: 16, label: `全勤天数`, visible: true },
        { key: 17, label: `出勤工资`, visible: true },
        { key: 18, label: `专项补贴`, visible: true },
        { key: 19, label: `应发工资`, visible: true },
        { key: 20, label: `社保`, visible: true },
        { key: 21, label: `个税`, visible: true },
        { key: 22, label: `罚款`, visible: true },
        { key: 23, label: `其他`, visible: true },
        { key: 24, label: `实发工资`, visible: true },
        { key: 25, label: `内部备注`, visible: true },
        { key: 26, label: `备注`, visible: true },
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      checkform:{
        renewalDate:'',
        company:'玉山分公司'
      },
      checkformrules:{},
      dateshow:false,
      num:null,
    saveform:{
      renewalDate:''
    },
      btndisabled:true
    };
  },
  created() {
    this.getList();
  },
  methods: {
    opendate(val){
       this.dateshow=true
      this.num=val
    },
   async btndate(){


      this.dateshow=false
      if(this.num==1){

        await this.handleupdatecheck()
        await this.getList()
        this.btndisabled=false
      }else if(this.num==2){
       await  this.handleupdatepay()
        await this.getList()
      }else if(this.num==3){
        await this.hanleSave()
      }else if(this.num==4){
        await this.handleSend()
      }
      else if(this.num==5){
        this.checkform.company="玉山分公司"
        await this.handleExport()
      }
    },
    closedate(){
      this.dateshow=false
      this.checkform.renewalDate=""
      this.num=null
    },
    async handleupdatecheck(){
      this.checkform.renewalDate=this.newDate(this.checkform.renewalDate).substr(0,7)
      console.log( this.checkform.renewalDate)
     await updatecheckapi(this.checkform).then(res=>{
         if(res){
           this.$message.success(`${res.msg}`)
         }
      })
    },
  async  handleupdatepay(){
      this.checkform.renewalDate=this.newDate(this.checkform.renewalDate).substr(0,7)
     await updatepayapi(this.checkform).then(res=>{
        if(res){
          this.$message.success(`${res.msg}`)
        }
      })
    },
    async hanleSave(){
      this.checkform.renewalDate=this.newDate(this.checkform.renewalDate).substr(0,7)
      await saverecord(this.checkform).then(res=>{
        this.$message.success(`${res.msg}`)
      })
    },
    async handleSend(){
      this.checkform.renewalDate=this.newDate(this.checkform.renewalDate).substr(0,7)
      await sendinfo(this.checkform).then(res=>{
        this.$message.success(`${res.msg}`)
      })
  },
    handleupdatestaff(){
      updatestaffwagelist().then(res=>{
        this.$message.success(`${res.msg}`)
      })
    },
    newDate(time) {
      var date = new Date(time)
      var y = date.getFullYear()
      var m = date.getMonth() + 1
      m = m < 10 ? '0' + m : m
      var d = date.getDate()
      d = d < 10 ? '0' + d : d
      return y + '-' + m + '-' + d
    },
    /** 查询工资计算列表 */
    getList() {
      this.loading = true;
      listWageOperate(this.queryParams).then(response => {
        this.wageOperateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sequence: null,
        departmentName: null,
        departmentId: null,
        company: null,
        postId: null,
        postName: null,
        staffName: null,
        staffId: null,
        entryDate: null,
        leaveDate: null,
        staffState: null,
        staffType: null,
        basiWage: null,
        postAllowance: null,
        loyaltyReward: null,
        skillAssessment: null,
        managementAssessment: null,
        safetyAward: null,
        performance: null,
        fullAttendance: null,
        attendanceDays: null,
        attendanceWage: null,
        specialSubsidy: null,
        payDue: null,
        socialSecurity: null,
        personalIncomeTax: null,
        fine: null,
        other: null,
        realWages: null,
        internalNotes: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工资计算";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWageOperate(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工资计算";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWageOperate(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWageOperate(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除工资计算编号为"' + ids + '"的数据项？').then(function() {
        return delWageOperate(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.checkform.renewalDate=this.newDate(this.checkform.renewalDate).substr(0,7)
      this.download('module/wageOperate/export', {
        ...this.checkform
      }, `wageOperate_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
